home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Examples / AppKit / ColorTest / colorAsAscii.m < prev    next >
Encoding:
Text File  |  1992-05-30  |  2.9 KB  |  104 lines

  1. /*
  2.  
  3. The following two functions let you convert
  4. between a color and an ASCII string suitable
  5. for writing to defaults or storing in text documents.
  6.  
  7. Call convertColorToString() with a color and a
  8. string at least COLORSTRINGLENGTH long. Later
  9. call getColorFromString() with a string obtained
  10. from convertColorToString() and a pointer to a
  11. color; if the function returns YES a color was
  12. successfully parsed from the string.
  13.  
  14.  void convertColorToString (NXColor color, char *str)
  15.  BOOL getColorFromString (const char *str, NXColor *color)
  16.  
  17. The following two functions let you read/write colors
  18. in the defaults database of the application.
  19.  
  20.  void writeColorToDefaults (NXColor color, const char *defaultName)
  21.  BOOL readColorFromDefaults (const char *defaultName, NXColor *color)
  22.  
  23. Written by Ali Ozer, 5/29/92.
  24.  
  25. You may freely copy, distribute and reuse the code in this example.
  26. NeXT disclaims any warranty of any kind, expressed or implied,
  27. as to its fitness for any particular use.
  28.  
  29. */
  30.  
  31. #import <appkit/appkit.h>
  32. #import "colorAsAscii.h"
  33.  
  34. void writeColorToDefaults (NXColor color, const char *defaultName)
  35. {
  36.     char str[1024];
  37.     convertColorToString(color, str);
  38.     NXWriteDefault([NXApp appName], defaultName, str);
  39. }
  40.  
  41. BOOL readColorFromDefaults (const char *defaultName, NXColor *color)
  42. {
  43.     const char *tmp = NXGetDefaultValue([NXApp appName], defaultName);
  44.     return (tmp && getColorFromString (tmp, color)) ? YES : NO;
  45. }
  46.  
  47. void convertColorToString (NXColor color, char *str)
  48. {
  49.     static const char hexDigits[16] = "0123456789ABCDEF";
  50.     NXStream *colorStream = NXOpenMemory(NULL, 0, NX_READWRITE);
  51.     NXTypedStream *ts = colorStream ? NXOpenTypedStream(colorStream, NX_WRITEONLY) : NULL;
  52.     
  53.     if (ts) {
  54.     int i, pos;
  55.     NXWriteColor(ts,color);
  56.     NXCloseTypedStream(ts);
  57.     pos = NXTell(colorStream);
  58.         NXSeek(colorStream, 0, NX_FROMSTART);
  59.     i = 0;
  60.     while (i++ < pos) {
  61.         unsigned char ch = NXGetc(colorStream);
  62.         *str++ = hexDigits[(ch>>4) & 0xF];
  63.         *str++ = hexDigits[ch & 0xF];
  64.         }
  65.     }
  66.     *str = 0;
  67.     if (colorStream) NXCloseMemory (colorStream, NX_FREEBUFFER);
  68. }
  69.  
  70. #define BAD 255
  71. #define HEX(c) (((c)>='A' && (c)<='F') ? ((c)-'A'+10): (((c)>='0'&&(c)<='9') ? ((c)-'0') : BAD))
  72.  
  73. BOOL getColorFromString (const char *str, NXColor *color)
  74. {
  75.     unsigned char binaryBuffer[COLORSTRINGLENGTH];
  76.     NXStream *stream;
  77.     NXTypedStream *ts;
  78.     int len = 0;
  79.     BOOL success = NO;
  80.     
  81.     while (*str) {
  82.     unsigned char first = HEX(str[0]), second = HEX(str[1]);
  83.     if (first == BAD || second == BAD) return NO;    
  84.     binaryBuffer[len] = (first << 4) + second;
  85.     str += 2;
  86.     len++;
  87.     }
  88.     
  89.     if (len &&
  90.     (stream = NXOpenMemory(binaryBuffer, len, NX_READONLY)) &&
  91.         (ts = NXOpenTypedStream(stream, NX_READONLY))) {
  92.     NX_DURING
  93.         *color = NXReadColor(ts);
  94.         success = YES;
  95.     NX_HANDLER
  96.     NX_ENDHANDLER     
  97.     }
  98.     if (ts) NXCloseTypedStream(ts);
  99.     if (stream) NXCloseMemory(stream, NX_SAVEBUFFER);
  100.  
  101.     return success;
  102. }
  103.  
  104.